﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class ForumsBLL : CommonBLL<ForumsInfo, ForumsDAL>
    {
        public static IQueryable<ForumsInfo> GetList(string search,bool isDisHidden=false)
        {
            var list = GetList();
            if (!isDisHidden)
                list = list.Where(o => o.Status == 0);
            if (!string.IsNullOrEmpty(search))
                list = list.Where(o => o.Name.Contains(search));
            return list;
        }

        public static ForumsInfo Get(int id)
        {
            using (var dal = new ForumsDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                if (re != null)
                {
                    if (re.LastPosterId == 0 || re.LastPostTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                    {
                        re.TodayPostCount = 0;
                        re.TodayTopicCount = 0;
                    }
                }
                return re;
            }
        }

        public static bool Edit(ForumsInfo info)
        {
            using (var dal = new ForumsDAL())
            {
                if (info.LastPosterId == 0 || info.LastPostTime.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    info.TodayPostCount = 0;
                    info.TodayTopicCount = 0;
                }
                return dal.Update(info) != null;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new ForumsDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
